﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using QuanLyHocPhi.Entity;

/// <summary>
/// Summary description for VAITRO_CHUCNANGDAO
/// </summary>
namespace QuanLyHocPhi.DAL
{
    public class VAITRO_CHUCNANG_DAO
    {
        private const string SQL_SELECT_ALL = "SELECT * FROM VAITRO_CHUCNANG";
        private const string SQL_INSERT = "insert into VAITRO_CHUCNANG(MA_VAI_TRO, MA_CHUC_NANG) values (@MA_VAI_TRO, @MA_CHUC_NANG)";
        private const string SQL_DELETE = "DELETE FROM VAITRO_CHUCNANG WHERE MA_VAI_TRO = @MA_VAI_TRO and MA_CHUC_NANG = @MA_CHUC_NANG";

        // Parameters
        private const string PARM_VAI_TRO_ID = "@MA_VAI_TRO";
        private const string PARM_CHUC_NANG_ID = "@MA_CHUC_NANG";

        #region Public Methods
        public IList<VAITRO_CHUCNANG> GetAll()
        {
            IList<VAITRO_CHUCNANG> VAITRO_CHUCNANGs = new List<VAITRO_CHUCNANG>();
            using (SqlDataReader reader = SQLDBHelper.ExecuteReader(SQLDBHelper.CONN_STRING,
                CommandType.Text, SQL_SELECT_ALL, null))
            {
                while (reader.Read())
                {
                    VAITRO_CHUCNANG VAITRO_CHUCNANG = new VAITRO_CHUCNANG();
                    VAITRO_CHUCNANG.MA_VAI_TRO.MA_VAI_TRO = reader.GetInt32(reader.GetOrdinal("MA_VAI_TRO"));
                    VAITRO_CHUCNANG.MA_CHUC_NANG.MA_CHUC_NANG = reader.GetInt32(reader.GetOrdinal("MA_CHUC_NANG"));
                    VAITRO_CHUCNANGs.Add(VAITRO_CHUCNANG);
                }
            }
            return VAITRO_CHUCNANGs;
        }

        public Boolean Insert(VAITRO_CHUCNANG VAITRO_CHUCNANG)
        {
            {
                SqlParameter[] parms;
                parms = new SqlParameter[] {
											 new SqlParameter(PARM_VAI_TRO_ID, SqlDbType.Int),
                                             new SqlParameter(PARM_CHUC_NANG_ID, SqlDbType.Int)};
                parms[0].Value = VAITRO_CHUCNANG.MA_VAI_TRO;
                parms[0].Value = VAITRO_CHUCNANG.MA_CHUC_NANG;

                try
                {
                    int result = 0;
                    SqlConnection conn = new SqlConnection(SQLDBHelper.CONN_STRING);
                    result = SQLDBHelper.ExecuteNonQuery(conn,
                        CommandType.Text, SQL_INSERT, parms);
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                    return (result > 0 ? true : false);
                }
                catch (Exception e)
                {
                    throw e;
                }
                //return false;
            }
        }

        public bool Delete(int MA_VAI_TRO, int MA_CHUC_NANG)
        {
            SqlParameter[] parms;
            parms = new SqlParameter[] {
											 new SqlParameter(PARM_VAI_TRO_ID, SqlDbType.Int),
                                             new SqlParameter(PARM_CHUC_NANG_ID, SqlDbType.Int)};
            parms[0].Value = MA_VAI_TRO;
            parms[0].Value = MA_CHUC_NANG;

            try
            {
                int result = 0;
                result = SQLDBHelper.ExecuteNonQuery(SQLDBHelper.CONN_STRING,
                    CommandType.Text, SQL_DELETE, parms);
                return (result > 0 ? true : false);
            }
            catch (Exception e)
            {
                throw e;
            }
            //return false;
        }
        #endregion
    }
}